package com.vxdata.common.utils.逆向工程;


import com.vxdata.common.utils.逆向工程.vo.ReligionReportExcelVo;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;

public class SwaggerToExcelUtil {
    public static void main(String[] args) {
        // 获取 ReligionReportQvo 类的所有字段
        Field[] fields = ReligionReportExcelVo.class.getDeclaredFields();
        // System.out.println(swaggerToExcel(fields, "ReligionReportExcelVo"));
        System.out.println(swaggerToExcelMap(fields));
    }

    /**
     * 替换 Swagger 注解为 Excel 注解
     *
     * @param fieldList
     * @param className
     * @return
     */
    private static String swaggerToExcel(Field[] fieldList, String className) {
        StringBuilder str = new StringBuilder();
        str.append("public class " + className + "  {\n");

        for (Field f : fieldList) {
            // 获取字段上的 @ApiModelProperty 注解
            ApiModelProperty apiModelProperty = f.getAnnotation(ApiModelProperty.class);
            if (apiModelProperty == null) {
                continue;
            }
            String value = apiModelProperty.value();

            // 设置字段可访问
            f.setAccessible(true);
            String fieldName = f.getName();
            String fileType = f.getType().getSimpleName();

            str.append("\t\t@Excel(name = \"" + value + "\")\n");
            str.append("\t\t@ApiModelProperty(value = \"" + value + "\")\n");
            str.append("\t\tprivate " + fileType + " " + fieldName + ";\n");
            str.append("\n");
        }
        str.append("}");
        return str.toString();
    }

    /**
     * 替换 Swagger 注解为 Map
     *
     * @param fieldList
     * @return
     */
    private static String swaggerToExcelMap(Field[] fieldList) {
        StringBuilder str = new StringBuilder();
        str.append("Map<String, Object> dataMap = new HashMap<>();\n");

        for (Field f : fieldList) {
            // 设置字段可访问
            f.setAccessible(true);
            String fieldName = f.getName();
            String fileType = f.getType().getSimpleName();
            String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

            str.append("dataMap.put(\"" + fieldName + "\", vo." + methodName + "());\n");
        }
        return str.toString();

    }


}
